// pages/integralMall/submitOrder/submitOrder.js
const app = getApp()
import api from '../../../utils/api'
let apis = api.api
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		imageURL: 'https://cdn.oss.dgddaijia.com/icons/shop4.png',
		buyNum: 1,
		title: '',
		score: 0,
		shopId: null,
		price: 0,
		pic: '',
		host: app.globalData.host,
		userDefAdr: [],
	},
	goAddress() {
		wx.navigateTo({
			url: '/pages/personal/manageAdress/index/index'
		})
	},
	// getData(options) {
	// 	
	// },
	goPay() {
		// https://dageda.api.hjdz.ltd/goods/order
		var that = this
		let url = apis.host + 'goods/order';
		var timestamp = Date.parse(new Date());
		timestamp = (timestamp / 1000).toString();
		let data = {
			coupon_id: '',
			/* 否	string	券ID */
			gid: that.data.shopId,
			/* 是	string	商品ID */
		}
		apis.request(url, data, 'POST').then(function(res) {
			wx.showToast({
				title: '下单成功',
				icon: 'success'
			})
			let data = res.data;
			let nonceStr = data.nonce_str.toString();
			let packages = data.prepay_id.toString();
			let paySign = data.paySign.toString();
			// var paysign = res.data.appid + '\n' + timestamp + '\n' + nonceStr + '\n' + 'prepay_id=' +
			// 	packages + '\n';
			// console.log('paysign:', paysign)
			// paysign = apis.sha256(paysign);
			// let paysigns = apis.base64_encode(paysign)
			// console.log('paysign:', paysign)
			// console.log('paysigns:', paysigns)
			let option = {
				timeStamp: res.data.timeStamp.toString(),
				nonceStr: nonceStr,
				package: 'prepay_id=' + packages,
				signType: 'MD5',
				paySign: paySign,
				success(res) {
					console.log('支付成功', res)
					wx.showToast({
						title: '支付成功',
						icon: 'success'
					})
					setTimeout(function(res) {
						wx.navigateTo({
							url: '/pages/integralMall/mallList/mallList'
						})
					}, 1000)
				},
				fail(res) {
					wx.showToast({
						title: '支付失败,请重试',
						icon: 'none'
					})
					console.log('支付失败', res)
				}
			};
			// 小程序appId：appid
			// 时间戳
			// 随机字符串 nonce_str
			// 订单详情扩展字符串 prepay_id

			console.log('ddddd', option)
			wx.requestPayment(option)
			console.log('aaaaa', option)
		}).catch(function(res) {
			wx.showToast({
				title: '下单失败,请重试',
				icon: 'none'
			})
			console.log('下单失败', res)
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		// that.getData(options)
	},
	onShow() {
		var that = this;
		let pages = getCurrentPages();
		// 数组中索引最大的页面--当前页面
		let currentPage = pages[pages.length - 1];
		// 打印出当前页面中的 options
		console.log(currentPage.options)
		// https://dageda.api.hjdz.ltd/goods/orderList
		that.setData({
			shopId: currentPage.options.s_id,
			buyNum: currentPage.options.s_num,
			title: currentPage.options.s_tit,
			score: currentPage.options.s_score,
			price: currentPage.options.s_price,
			pic: currentPage.options.pic,
		})
		let url = apis.host + 'goods/orderList'
		let getDefaulUrl = apis.host + 'member/default_address';
		let data = {}
		apis.request(url, data, 'POST').then(function(res) {
			console.log('获取订单成功', res)
		}).catch(function(res) {
			console.log('获取失败', res)
		})
		apis.request(getDefaulUrl, data, 'POST').then(function(res) {
			console.log('获取默认地址成功', res.data)
			that.setData({
				userDefAdr: res.data
			})
		})
	},
})
